using Manager;
using UnityEngine;

namespace Player
{
    /// <summary>
    /// 玩家基本控制类
    /// </summary>
    public class PlayerLocomotion :MonoBehaviour
    {
        
        //输入系统
        private InputManager inputManager;

        //位置组件
        private Transform trans;
        
        //刚体组件
        private Rigidbody rigid;
        
        //相机控制
        private PlayerCameraController playerCameraController;
        
        //动画控制
        private PlayerAnimatorController playerAnimatorController;


        private void Awake()
        {
            inputManager=InputManager.Instance;
            trans = transform;
            rigid = GetComponent<Rigidbody>();
            playerCameraController = GetComponentInChildren<PlayerCameraController>();
            playerAnimatorController = GetComponentInChildren<PlayerAnimatorController>();
        }


        //总更新函数
        public void HandleUpdate(float delta)
        {
            //获得方向
            var cameraTrans = playerCameraController.transform;
            Vector3 direction = (cameraTrans.forward * inputManager.originMoveForward +
                                cameraTrans.right * inputManager.originMoveRight).normalized;
            
            Turn(direction,delta);
            Move(direction,delta);
            Catch();
            Interact();

        }
        
        
        //转向函数
        private void Turn(Vector3 direction,float delta)
        {
            if (direction.magnitude >=0.01f)
            {
                Quaternion lookAt = Quaternion.LookRotation(direction);

                trans.rotation = Quaternion.Slerp(transform.rotation, lookAt, PlayerConstant.TurnSpeed * delta);
            }
        }
                
        
        
        
        //移动函数
        private void Move(Vector3 direction,float delta)
        {
            //设置速度
            Vector3 velocity = direction * ((inputManager.isAiming ? PlayerConstant.AimSpeed:PlayerConstant.MoveSpeed) * playerAnimatorController.PlayMoveAnimation(inputManager.isSprintf,inputManager.moveMagnitude,delta));
            rigid.velocity = new Vector3(velocity.x, rigid.velocity.y, velocity.z);
            
            
        }
        
        
        
        //捕捉函数
        private void Catch()
        {
            //播放动画
            playerAnimatorController.PlayAimAnimation(inputManager.isAiming);
            
        }
        
        //交互函数
        private void Interact()
        {
            
            if (inputManager.isInteract)
            {
                
                
            }
            
        }
        
       
    }
}